package yunwo.cn.yzstation.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Looper;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Toast;



import yunwo.cn.yzstation.base.Yzapp;


/**
 * 界面工具类
 * 
 * @author joial
 *
 */
public class UIUtils {

	public static Context getContext() {
		return Yzapp.getInstance();
	}

	public static Thread getMainThread() {
		return Yzapp.getMainThread();
	}

	public static long getMainThreadId() {
		return Yzapp.getMainThreadId();
	}
	public static Activity indextActivity = null;
	/** dip转换px */
	public static int dip2px(int dip) {
		final float scale = getContext().getResources().getDisplayMetrics().density;
		return (int) (dip * scale + 0.5f);
	}

	/** px转换dip */
	public static int px2dip(int px) {
		final float scale = getContext().getResources().getDisplayMetrics().density;
		return (int) (px / scale + 0.5f);
	}

/*	*//** 获取主线程的handler *//*
	public static Handler getHandler() {
		// 获得主线程的looper
		Looper mainLooper = BaseApplication.getMainThreadLooper();
		// 获取主线程的handler
		Handler handler = new Handler(mainLooper);
		return handler;
	}

	public static Looper getMainLooper() {
		return BaseApplication.getMainThreadLooper();
	}*/

	/** 延时在主线程执行runnable */
/*	public static boolean postDelayed(Runnable runnable, long delayMillis) {
		return getHandler().postDelayed(runnable, delayMillis);
	}*/

	/**
	 * 退出应用
	 * @param activity
	 */
	/*public static void exitApp(Activity activity){
		Intent intent = new Intent(activity, IndexActivity.class);
		intent.putExtra(IntentTag.EXIT_APP, true);
		activity.startActivity(intent);
		BaseActivity.finishActivity(LoginActivity.class);
	}*/
/*

	*/
/** 在主线程执行runnable *//*

	public static boolean post(Runnable runnable) {
		return getHandler().post(runnable);
	}

	*/
/** 从主线程looper里面移除runnable *//*

	public static void removeCallbacks(Runnable runnable) {
		getHandler().removeCallbacks(runnable);
	}
*/

	public static View inflate(int resId) {
		return LayoutInflater.from(getContext()).inflate(resId, null);
	}

	/** 获取资源 */
	public static Resources getResources() {
		return getContext().getResources();
	}

	/** 获取文字 */
	public static String getString(int resId) {
		return getResources().getString(resId);
	}

	/** 获取文字数组 */
	public static String[] getStringArray(int resId) {
		return getResources().getStringArray(resId);
	}

	/** 获取dimen */
	public static int getDimens(int resId) {
		return getResources().getDimensionPixelSize(resId);
	}

	/** 获取drawable */
	public static Drawable getDrawable(int resId) {
		return getResources().getDrawable(resId);
	}

	/** 获取颜色 */
	public static int getColor(int resId) {
		return getResources().getColor(resId);
	}

	/** 获取颜色选择器 */
	public static ColorStateList getColorStateList(int resId) {
		return getResources().getColorStateList(resId);
	}

	public static boolean isRunInMainThread() {
		return android.os.Process.myTid() == getMainThreadId();
	}

	public static void runInMainThread(Runnable runnable) {
		if (isRunInMainThread()) {
			runnable.run();
		} else {
//			post(runnable);
		}
	}

	/** 对toast的简易封装。线程安全，可以在非UI线程调用。 */
	public static void showToastSafe(final int resId) {
		showToastSafe(getString(resId));
	}

	/** 对toast的简易封装。线程安全，可以在非UI线程调用。 */
	public static void showToastSafe(final String str) {
		if (isRunInMainThread()) {
			Toast.makeText(getContext(), str, Toast.LENGTH_SHORT).show();
		} else {
			/*post(new Runnable() {
				@Override
				public void run() {
					Toast.makeText(getContext(), str, Toast.LENGTH_SHORT)
							.show();
				}
			});*/
		}
	}
	private static long lastClickTime;
	public static boolean isFastDoubleClick() {
		long time = System.currentTimeMillis();
		long timeD = time - lastClickTime;
		if (0 < timeD && timeD < 200) {
			return true;
		}
		lastClickTime = time;
		return false;
	}
}
